package com.alibaba.japan.club.runtime.springmvc.exception;

import org.springframework.core.NestedRuntimeException;

import com.alibaba.japan.club.runtime.springmvc.util.StackTraceUtil;

/**
 * 自定义异常<br>
 * 所有的使用到spring mvc框架的web工程都是抛出些异常的子类<br>
 * 此类中的errorCode属性定义了异常的分类,可以在applicationContext.xml中配置异常定制化页面的跳转
 * 
 * @author:shaochuan.wangsc
 * @date:2009-3-24
 * 
 */
public abstract class SpringMVCException extends NestedRuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2937776799790568995L;

	public static final String DEFAULT_ERROR_CODE = "0";

	private Throwable cause;

	/**
	 * Constructor for SpringMVCException.
	 * 
	 * @param msg the detail message
	 */
	public SpringMVCException(String msg) {
		super(msg);
	}

	/**
	 * Constructor for SpringMVCException.
	 * 
	 * @param msg the detail message
	 * @param cause the root cause
	 */
	public SpringMVCException(String msg, Throwable cause) {
		super(msg, cause);
		this.cause = cause;
	}

	public String getMessage() {
		StringBuilder sb = new StringBuilder("errorCode:" + getErrorCode() + "," + super.getMessage());
		if (cause != null) {
			sb.append("; nested exception is:");
			sb.append(System.getProperty("line.separator"));
			sb.append(cause.toString());
			sb.append(System.getProperty("line.separator"));
			sb.append(StackTraceUtil.getStackTrace(cause));
		}
		return sb.toString();
	}



	public String toString() {
		return getClass().getName() + ": " + getMessage();
	}

	/**
	 * 取出异常的错误号
	 * 
	 * @return
	 */
	public abstract String getErrorCode();

	/**
	 * 设置异常的错误号
	 */
	public abstract void setErrorCode(String errorCode);
}
